package com.jn.erp.wechat.student.api.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jn.erp.common.core.result.AjaxResult;
import com.jn.erp.web.api.student.entity.StudentUser;
import com.jn.erp.web.api.student.service.StudentUserService;
import com.jn.erp.wechat.common.api.controller.base.BaseWxAuthController;
import com.jn.erp.wechat.common.api.entity.dto.WxUserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * <pre>
 *
 * </pre>
 *
 * @author sunjiyong
 * @since 2024/9/10 17:48
 */

@RestController
@RequestMapping("/wxs/my")
public class StudentMpMyController extends BaseWxAuthController {

    @Autowired
    private StudentUserService studentUserService;

    //获取用户信息
    @PostMapping("/getInfo")
    @Transactional
    public AjaxResult getInfo() {
        AjaxResult ajax = AjaxResult.success();
        //获取登录家长端相关信息
        WxUserDto studentUser = getStudentUser();
        Long userId = studentUser.getUserId();
        if (userId == null) {
            return AjaxResult.error("内容获取失败");
        }
        //获取用户信息
        StudentUser parent = studentUserService.getById(userId);
        ajax.put(AjaxResult.DATA_TAG, parent);
        return ajax;
    }

    //修改昵称
    @PostMapping("/updateNickName")
    @Transactional
    public AjaxResult updateNickName(@RequestBody StudentUser parent) {
        //获取登录家长端相关信息
        WxUserDto studentUser = getStudentUser();
        Long userId = studentUser.getUserId();
        if ( userId == null) {
            return AjaxResult.error("内容获取失败");
        }
        UpdateWrapper<StudentUser> up=new UpdateWrapper<>();
        up.set("name",parent.getName());
        up.eq("id",userId);
        boolean update = studentUserService.update(up);
        if (update){
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }




}
